package it.unibz.easytravel.travel;

import java.util.ArrayList;
import it.unibz.easytravel.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class SelectTravellers extends Activity {
	
	public static final String PERSON_IDS = "person_ids";
	
	private LinearLayout travelersList;
	
	private Button addButton;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.travel_selecttravelers);

		travelersList = (LinearLayout) findViewById(R.id.travel_select_travelers_list);
		
		Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null,
				null, null);
		
		startManagingCursor(cursor);
		int nameColumn = cursor
				.getColumnIndexOrThrow(android.provider.Contacts.People.NAME);
		
		int idColumn = cursor.getColumnIndexOrThrow(android.provider.Contacts.People._ID);
		
		Intent i = getIntent();
		
		ArrayList<Integer> personIds = i.getExtras().getIntegerArrayList(PERSON_IDS); 
		
		if (cursor.moveToFirst()) {
			do {
				String name = cursor.getString(nameColumn);
				int id = cursor.getInt(idColumn);
				CheckBox travelerCheckBox = new CheckBox(this);
				travelerCheckBox.setText(name);
				travelerCheckBox.setId(id);
				if (personIds.contains(id)) {
					travelerCheckBox.setChecked(true);
				}
				travelersList.addView(travelerCheckBox);
			} while (cursor.moveToNext());
		}
		
		addButton = (Button) findViewById(R.id.travel_add_travelers_button);
		addButton.setOnClickListener(addTravelersListener);
		
	}
	
	private OnClickListener addTravelersListener = new OnClickListener() {
		
		public void onClick(View v) {
			
			ArrayList<Integer> travelerIds = new ArrayList<Integer>();
			for (int i = 0; i < travelersList.getChildCount(); i++) {
				CheckBox cbox = (CheckBox) travelersList.getChildAt(i);
				if (cbox.isChecked()) {
					travelerIds.add(cbox.getId());
				}
			}
			Bundle bundle = new Bundle();
			bundle.putIntegerArrayList(PERSON_IDS, travelerIds);
			Intent mIntent = new Intent();
			mIntent.putExtras(bundle);			
			setResult(RESULT_OK, mIntent);
			finish();
		}
	};
	
	
	
//	/**
//	 * @return the list of travelers those goes into the travel
//	 */
//	public Vector<Traveller> getTravelersList(){
//		travelers = new Vector<Traveller>();
//		Cursor C = getContentResolver().query(People.CONTENT_URI, null, null,
//				null, null);
//		startManagingCursor(C);
//		int nameColumn = C
//				.getColumnIndexOrThrow(android.provider.Contacts.Phones.NAME);
//
//		if (C.moveToFirst()) {
//			do {
//				travelers.addElement(new Traveller(C.getString(nameColumn)));
//			} while (C.moveToNext());
//		}
//		return travelers;
//	}
	
	/**
	 * 
	 * @param The name of the traveler
	 * @return the Traveler Object or null if the traveler does nor exist
	 */
//	public Traveller getTraveler(String name){
//		int travelers_size = travelers.size();
//		for (int i = 0; i < travelers_size; i++) {
//			if(((Traveller)travelers.elementAt(i)).getName().equals(name))
//				return travelers.elementAt(i);
//		}
//		return null;
//		
//	}
//	
//	public String toString(){
//		int travelers_size = travelers.size();
//		String str="";
//		for (int i = 0; i < travelers_size; i++)
//			str+=travelers.elementAt(i).toString();
//
//		return str;
//	}
}
